package com.roast.services.my;

import com.roast.util.QueryObject;
import com.roast.domain.*;
import com.roast.util.*;

public class LoginIMPL extends GeneralSO implements Login {

    private UserData user;
    private QueryObject queryObject;
    private DomainObject fromDB;

    @Override
    public int login(UserData user) {
        return new LoginIMPL().generalExecution(user);
    }

    @Override
    protected int executeSO(Object obj) {
        if (!setUser(obj)) {
            return 10201;
        }
        if (!user.isEmailSet()) {
            return 10202;
        }
        if (!setQuery()) {
            return 10203;
        }
        if (!isUserExist()) {
            return 10204;
        }
        if (!user.isPasswordSet()) {
            return 10205;
        }
        if (!checkPassword()) {
            return 10206;
        }
        if (!copyObject(obj)) {
            return 10207;
        }
        return 10200;
    }

    private boolean setUser(Object obj) {
        try {
            this.user = (UserData) obj;
            return true;
        } catch (ClassCastException ex) {
            return false;
        }
    }

    private boolean setQuery() {
        queryObject = QueryBeanFactory.getBean("readUserByParametar");
        if (queryObject == null) {
            return false;
        }
        return queryObject.addParametar("femail", user.getEmail());
    }

    private boolean isUserExist() {
        fromDB = dbbr.readOneByParametar(queryObject);
        return queryObject.getSignal() == 10030;
    }

    private boolean checkPassword() {
        String passwordFromDB = ((UserData) fromDB).getPassword();
        return (user.getPassword().equals(passwordFromDB));
    }

    private boolean copyObject(Object obj) {
        try {
            return ((UserData) obj).copyObject(fromDB);
        } catch (ClassCastException ex) {
            return false;
        }
    }

    @Override
    protected boolean executeState(int signal) {
        switch (signal) {
            case 10200:
                showOperationState(signal, "_login: You have successfully login.");
                return true;
            case 10201:
                showOperationState(signal, "_login:ERROR: Class cast exception.");
                return false;
            case 10202:
                showOperationState(signal, "_login:ERROR: You did not set user email address.");
                return false;
            case 10203:
                showOperationState(signal, "_login:ERROR: Fail to set query.");
                return false;
            case 10204:
                showOperationState(signal, "_login:ERROR: Try with different email address.");
                return false;
            case 10205:
                showOperationState(signal, "_login:ERROR: You did not set user password.");
                return false;
            case 10206:
                showOperationState(signal, "_login:ERROR: Wrong password.");
                return false;
            case 10207:
                showOperationState(signal, "_login:ERROR: Fail to copy object.");
                return false;
        }
        return false;
    }
}
